package io.gitee.tooleek.lock.spring.boot.factory;

import java.util.EnumMap;

import io.gitee.tooleek.lock.spring.boot.enumeration.InterProcess;
import io.gitee.tooleek.lock.spring.boot.exception.ServiceNotFoundException;
import io.gitee.tooleek.lock.spring.boot.service.LockService;
import io.gitee.tooleek.lock.spring.boot.service.impl.zookeeper.MultiZkLockServiceImpl;
import io.gitee.tooleek.lock.spring.boot.service.impl.zookeeper.MutexZkLockServiceImpl;
import io.gitee.tooleek.lock.spring.boot.service.impl.zookeeper.ReadZkLockServiceImpl;
import io.gitee.tooleek.lock.spring.boot.service.impl.zookeeper.WriteZkLockServiceImpl;
import io.gitee.tooleek.lock.spring.boot.service.impl.zookeeper.SemaphoreMutexZkLockServiceImpl;
import io.gitee.tooleek.lock.spring.boot.util.SpringUtil;

public class ZkServiceBeanFactory implements ServiceBeanFactory {
	
	private static EnumMap<InterProcess, Class<?>> serviceMap = new EnumMap<>(InterProcess.class);

	static {
		serviceMap.put(InterProcess.MULTI, MultiZkLockServiceImpl.class);
		serviceMap.put(InterProcess.MUTEX, MutexZkLockServiceImpl.class);
		serviceMap.put(InterProcess.READ, ReadZkLockServiceImpl.class);
		serviceMap.put(InterProcess.WRITE, WriteZkLockServiceImpl.class);
		serviceMap.put(InterProcess.SEMAPHORE_MUTEX, SemaphoreMutexZkLockServiceImpl.class);
	}
	
	/**
	 * 根据锁类型获取相应的服务处理类
	 * 
	 * @param interProcess
	 * @return
	 * @throws ServiceNotFoundException
	 */
	@Override
	public LockService getService(Object lockType) {
		LockService lockService = (LockService) SpringUtil.getBean(serviceMap.get(lockType));
		if (lockService == null) {
			throw new ServiceNotFoundException();
		}
		return lockService;
	}
}
